var ua = window.navigator.userAgent;
var baseUrl = '/api';
var PLCOMMON = {

    LOAD: function (option) {
        $.ajax({
            type: option.type || "POST",
            url: baseUrl + option.url,
            data: option.data,
            contentType: option.contentType || 'application/x-www-form-urlencoded',
            dataType: "json",
            //async: option.option || false,
            timeout: option.timeout || 30000,
            success: function (result) {
                option.callback.apply(null, arguments);
            },
            error: function (error) {
                option.callback(error);
            },
        })
    },

    browserEnv: {
        isInWeixin: function () {
            return /MicroMessenger/i.test(ua);
        },
        isInAndroidApp: function () {
            return /answernAndroid/i.test(ua);
        },
        isInIOSApp: function () {
            return /answernIOS/i.test(ua);
        },
        isInApp: function () {
            return /answernApp/i.test(ua);
        },
        isInAndroid: function () {
            return /android/i.test(ua);
        },
        isInIOS: function () {
            return /iPhone/i.test(ua);
        },
    },

    App: {
        navButtons: function (arrayObj) {//右上角分享、客服、ocr
            Jockey.send("pailexiong", {key: 'navButtons', data: arrayObj});
        },
        share: function (share) {
            Jockey.send("pailexiong", {key: 'share', data: share});
        },
        ocr: function (ocr) {
            Jockey.send("pailexiong", {key: 'ocr'});
        },
        login: function () {//登录
            Jockey.send("pailexiong", {key: 'login'});
        },
        date: function () {//显示原生时间空间
            Jockey.send("pailexiong", {key: 'date'});
        },
        location: function () {//获取位置
            Jockey.send("pailexiong", {key: 'location'});
        },
        image: function () {
            Jockey.send("pailexiong", {key: 'image'});
        },
        toNativePage: function (page) {
            location.href = page;
        },
        wxAuth: function () {
            Jockey.send("pailexiong", {key: 'wxAuth'});
        },
        hideNav: function (statusBarColor) {
            Jockey.send("pailexiong", {key: 'hideNav', statusBarColor: statusBarColor});
        },
        back: function (type) {
            Jockey.send("pailexiong", {key: 'back', type: type + ""});
        },
    },

    showThisPage: function (target, thisId) {
        $(target).addClass("hide");
        $(thisId).removeClass("hide");
    },

    showAlert: function (target, text, callback) {
        if (!text) {
            text = "操作失败，请稍后再试！";
        }
        $(target + " #model-alert").removeClass("hide").find(".alert-value").text(text);

        $("#alert-btn").on("click", function () {
            $(target + " #model-alert").addClass("hide");
            if (callback) {
                callback();
            }
        });
    },

    showConfirm: function (target, text) {
        if (!text) {
            text = "操作失败，请稍后再试！";
        }
        $(target + " #model-confirm").removeClass("hide").find(".alert-value").text(text);
    },

    isVaildMobile: function (mobile) {
        return /^1[3|4|5|7|8]\d{9}/ig.test(mobile);
    },
    isIdCard: function (idCard) {
        return /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/.test(idCard);
    },
    isPassport: function (passport) {
        return /^((P\d{7})|(E\d{8})|(G\d{8}))$/.test(passport);
    },
    isVaildName: function (name) {
        return /^[\u4E00-\u9FA5]{2,20}$/.test(name);
    },
    isCertificate: function (certificate) {
        return /^[a-zA-Z0-9]{7,21}$/.test(certificate);
    },
    isUppercaseNumber: function (value) {
        return /^[A-Z0-9]{8,50}$/.test(value);
    },
    isValidISO: function (ISO) {
        return /^[a-zA-Z]{3}\d{12}$/.test(ISO);
    },
    isCarNumber: function (carNumber) {
        return /^[\u4e00-\u9fa5]{1}[A-Z]{1}[A-Z_0-9]{5}$/.test(carNumber);
    },
    isVaildEmail: function (email) {
        return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/.test(email);
    },
    showAxKeyboard: function (target, thisTag, length, isIo) {
        $(target).removeClass("hide");
        if (isIo && isIo != 'engine') {
            $(".qio").addClass("disabled").attr("disabled", true);
        } else if (isIo == 'engine') {
            $(".q").removeClass("disabled").removeAttr("disabled");
        }
        $("#keyboard span").off().on("click", function () {
            var thisVal = $(thisTag).text();
            if (thisVal.length >= length && !$(this).hasClass("delete")) {
                return;
            }
            var dis = $(this).attr("disabled");
            var clazz = $(this).hasClass("delete");
            if (dis) {
                return;
            }
            if (clazz) {
                $(thisTag).text(thisVal.substr(0, thisVal.length - 1));
                return;
            }
            $(thisTag).text(thisVal + $(this).text());
        });
    },
    showIconText: function (iconTitle, iconText) {
        var div = '';

        function eachText() {
            for (var i = 0; i < iconText.length; i++) {
                div += "<div class='icon-conetnt'>";
                div += "<p>" + iconText[i].title + "</p>";
                div += "<p>" + iconText[i].content + "</p>";
                div += "</div>"
                if (i === iconText.length - 1) {
                    div += "<div class='icon-footer-btn' id='close-icon-model'>确定</div>"
                }

            }
            return div;
        }

        $("body").append("" +
            "<div class='icon-model'>" +
            "<div class='bg'></div>" +
            "<div class='icon-main'>" +
            "<div class='icon-title'>" + iconTitle + "</div>" +
            "<div class='icon-text'>" + eachText() + "</div>" +
            "</div>" +
            "</div>");

        $("#close-icon-model").on("click", function () {
            $(".icon-model").remove();
        });
    },
    getWxconfig: function (opts) {
        var data = {
            pageUrl: AXCOMMON.getUrlWithoutHash()
        };
        if (!opts) {
            opts = {
                title: "",
                imgUrl: ""
            }
        }
        opts.title = document.title;
        opts.imgUrl = location.origin + "/static/product/img/weixin_share.jpg";
        AXCOMMON.LOAD({
            url: '/weixin/Share',
            data: data,
            callback: function (result) {
                if (result.code == 0) {
                    result = result.data.body;
                }
                var jsApis = [
                    'onMenuShareTimeline',
                    'onMenuShareAppMessage',
                    'onMenuShareQQ',
                    'previewImage',
                    'chooseImage',
                    'uploadImage'
                ];
                var wxConfig = {
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: result.appId, // 必填，公众号的唯一标识
                    timestamp: result.timestamp, // 必填，生成签名的时间戳
                    nonceStr: result.nonceStr, // 必填，生成签名的随机串
                    signature: result.signature,// 必填，签名，见附录1
                    jsApiList: jsApis // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
                };
                wx.config(wxConfig);

                wx.ready(function () {
                    wx.onMenuShareTimeline(opts);
                    wx.onMenuShareAppMessage(opts);
                    wx.onMenuShareQQ(opts);
                });
            },
        })
    },
    getUrlWithoutHash: function () {
        var url = location.href;
        if (url.indexOf('#') === -1) {
            return url;
        }
        return url.substring(0, url.indexOf('#'));
    },

    setStore: function (key, value) {
        window.localStorage.setItem(key, value);
    },
    getStore: function (key) {
        return window.localStorage.getItem(key);
    },
    removeStore: function (key) {
        window.localStorage.removeItem(key);
    },

};

(function ($) {
    FastClick.attach(document.body);
    var Component = function () {
        return this;
    };
    Component.prototype = {
        init: function () {

            this.pageInit();
            this.bindEvent();
        },
        pageInit: function () {
            var _this = this;
            _this.setFontSize();


            // var doc = window.document;
            // var ua = navigator.userAgent;
            // var matches = ua.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i);
            // var UCversion = ua.match(/U3\/((\d+|\.){5,})/i);
            // var isUCHd = UCversion && parseInt(UCversion[1].split('.').join(''), 10) >= 80;
            // var isIos = navigator.appVersion.match(/(iphone|ipad|ipod)/gi);
            // var dpr = window.devicePixelRatio || 1;
            // if (!isIos && !(matches && matches[1] > 534) && !isUCHd) {
            //     dpr = 1;
            // }
            // var scale = 1 / dpr;
            //
            // var metaEl = doc.querySelector('meta[name="viewport"]');
            // if (!metaEl) {
            //     metaEl = doc.createElement('meta');
            //     metaEl.setAttribute('name', 'viewport');
            //     doc.head.appendChild(metaEl);
            // }
            // metaEl.setAttribute('content', 'width=device-width,user-scalable=no,initial-scale=' + scale + ',maximum-scale=' + scale + ',minimum-scale=' + scale);
            // doc.documentElement.style.fontSize = _baseFontSize / 2 * dpr * _fontscale;

        },
        bindEvent: function () {
            var _this = this;
            window.addEventListener('resize', _this.setFontSize);

            $("#back").off().on("click", function () {
                if (AXCOMMON.browserEnv.isInApp()) {
                    AXCOMMON.AnswernApp.back();
                } else {
                    window.history.back();
                }
            });

            if (location.origin == "https://m.95303.com") {
                var _hmt = _hmt || [];
                (function () {
                    var hm = document.createElement("script");
                    hm.src = "https://hm.baidu.com/hm.js?cde4a6bebd938aea9dfe9b1e14a086c0";
                    var s = document.getElementsByTagName("script")[0];
                    s.parentNode.insertBefore(hm, s);
                })();
                _hmt.push(['_trackPageview', location.href]);
                answernAccount= 'mobileWeb';
            } else if (location.origin == "https://weixin.95303.com") {
                var _hmt = _hmt || [];
                (function () {
                    var hm = document.createElement("script");
                    hm.src = "https://hm.baidu.com/hm.js?f5090b498b4176d421417db7d04a19f2";
                    var s = document.getElementsByTagName("script")[0];
                    s.parentNode.insertBefore(hm, s);
                })();
                _hmt.push(['_trackPageview', location.href]);
                answernAccount= 'weixin';
            }

            if ($.cookie && $.cookie('anxin_token')) {
                AXCOMMON.LOAD({
                    url: "/User/Get_userinfo",
                    callback: function (result) {
                        answernMemberId = result.data.tel;
                    }
                });
            }

        },

        setFontSize: function () {
            var designedWidth = 750;
            var actualWidth = Math.min(450, $(window).width());
            document.documentElement.style.fontSize = actualWidth * 100 / designedWidth + 'px';
        },

    };
    var component = new Component();
    component.init();
})(jQuery);




